# encoding:utf-8
class ArrayQueue:
    """
    基于环形数组实现的队列
    """
    def __init__(self, size: int):
        """构造方法"""
        self.nums = [0] * size
        self.__front = 0
        self.__rear = 0
        self.__size = 0

    def capacity(self) -> int:
        """获取队列的容量"""
        return len(self.nums)

    def size(self) -> int:
        """获取队列的长度"""
        return self.__size

    def is_empty(self) -> bool:
        """判断队列是否为空"""
        return self.__size == 0

    def push(self, num: int) -> None:
        """入队"""
        if self.__size == self.capacity():
            raise IndexError("队列已满")
        # 计算队尾指针，指向队尾索引 + 1
        # 通过取余操作，实现 rear 越过数组尾部后回到头部
        self.__rear = (self.__rear + 1) % self.capacity()
        self.nums[self.__rear] = num
        self.__size += 1

    def pop(self) -> int:
        """出队"""
        num = self.peek()
        # 队头指针向后移动一位，若越过尾部则返回到数组头部
        self.__front = (self.__front + 1) % self.capacity()
        self.__size -= 1
        return num

    def peek(self) -> int:
        """访问队首元素"""
        if self.is_empty():
            raise IndexError("队列为空")
        return self.nums[self.__front]

    def to_list(self) -> list[int]:
        """返回列表用于打印"""
        res = [0] * self.__size
        j: int = self.__front
        for i in range(self.__size):
            res[i] = self.nums[j]
            j = (j + 1) % self.capacity()
        return res
